﻿using System;
using System.Collections.Generic;

using Mono.Cecil;

using Flowgraph.Attributes;

namespace Flowgraph
{
    public class Engine
    {
        //---------------------------------------------------------------------------------------
        private static bool IsMonoRuntime = (Type.GetType("Mono.Runtime") != null);

        //---------------------------------------------------------------------------------------
        private static string[] m_CustomAttributeNameArray = new string[]
        {
            typeof(ExposeType).FullName,
            typeof(ExposeMethod).FullName,
        };

        //---------------------------------------------------------------------------------------
        private List<TypeDefinition> m_ExposedTypeList = new List<TypeDefinition>();
        private List<MethodDefinition> m_ExposedMethodList = new List<MethodDefinition>();

        private List<Listener> m_ListenerList = new List<Listener>();

        //---------------------------------------------------------------------------------------
        public void ScanAssembly(string lAssembly)
        {
            AssemblyDefinition lAssemblyDef = AssemblyDefinition.ReadAssembly(lAssembly);

            foreach (TypeDefinition lTypeDef in lAssemblyDef.MainModule.Types)
            {
                foreach (CustomAttribute lTypeAttribute in lTypeDef.CustomAttributes)
                {
                    if (lTypeAttribute.AttributeType.FullName.Equals(m_CustomAttributeNameArray[0]))
                    {
                        m_ExposedTypeList.Add(lTypeDef);
                        foreach (MethodDefinition lMethodDef in lTypeDef.Methods)
                        {
                            foreach (CustomAttribute lMethodAttribute in lTypeDef.CustomAttributes)
                            {
                                if (lMethodAttribute.AttributeType.FullName.Equals(m_CustomAttributeNameArray[1]))
                                {
                                    m_ExposedMethodList.Add(lMethodDef);
                                }
                            }
                        }
                    }
                }
            }
        }

        //---------------------------------------------------------------------------------------
        public void Trigger(object lSender, int lType, string lIdentification = null, int lCount = 0)
        {
        }
    }
}
